#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<unordered_map>
using namespace std;
int n;
struct node
{
	int id;
	int score;
	string s1;
}s[110];
int cmp(node a, node b)
{
	if (a.score == b.score) {
		return a.id < b.id;
	}
	else {
		return a.score > b.score;
	}
}
int main()
{
	map<string, int>mp;
	mp["rat"] = 6;
	mp["woman"] = mp["child"] = 5;
	mp["man"] = 4;
	mp["captain"] = 3;
	cin >> n;
	string s1;
	for (int i = 0; i < n; i++) {
		s[i].id = i + 1;
		cin >> s[i].s1;
		cin >> s1;
		s[i].score = mp[s1];
	}
	sort(s, s + n, cmp);
	for (int i = 0; i < n; i++) {
		cout << s[i].s1 << endl;
	}
	return 0;
}